﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using FitNesseRunner.Model;

namespace FitNesseRunner
{
	public class Folder : IFolder
	{
		private readonly DirectoryInfo _directoryInfo;
		private readonly IFolderTypeResolver _folderTypeResolver;

		public Folder(string path, IFolderTypeResolver folderTypeResolver)
			: this(new DirectoryInfo(path), folderTypeResolver)
		{
			_folderTypeResolver = folderTypeResolver;
		}

		public Folder(DirectoryInfo directoryInfo, IFolderTypeResolver folderTypeResolver)
		{
			_directoryInfo = directoryInfo;
			_folderTypeResolver = folderTypeResolver;
		}

		public string Name
		{
			get { return _directoryInfo.Name; }
		}

		public FolderType FolderType
		{
			get { return _folderTypeResolver.GetFolderType(_directoryInfo); }
		}

		public string Path
		{
			get { return _directoryInfo.FullName; }
		}

		internal class Manager : IFolderManager<Folder>
		{
			public Folder GetParent(Folder folder)
			{
				return folder._directoryInfo.Parent == null
				       	? null
				       	: new Folder(folder._directoryInfo.Parent, folder._folderTypeResolver);
			}

			public IEnumerable<Folder> GetChildren(Folder folder)
			{
				return from path in folder._directoryInfo.GetDirectories()
				       select new Folder(path, folder._folderTypeResolver);
			}
		}
	}
}
